package com.jeeplus.test.website.websiteFile.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.test.tools.resp.Convert2Bean;
import com.jeeplus.test.website.websiteFile.domain.entity.WebsiteFile;
import com.jeeplus.test.website.websiteFile.domain.vo.WebsiteFileReqVo;
import com.jeeplus.test.website.websiteFile.domain.vo.WebsiteFileRespVo;
import com.jeeplus.test.website.websiteFile.mapper.WebsiteFileMapper;
import com.jeeplus.test.website.websiteFile.service.WebsiteFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WebsiteFileServiceImpl extends ServiceImpl<WebsiteFileMapper, WebsiteFile> implements WebsiteFileService {
    @Autowired
    private WebsiteFileMapper websiteFileMapper;
    @Autowired
    private UserService userService;

    @Override
    public Page<WebsiteFileRespVo> getContentPage(WebsiteFileReqVo websiteFileReqVo, long current, long size) {
        WebsiteFile websiteFile = Convert2Bean.convertBean(websiteFileReqVo, WebsiteFile.class);
        LambdaQueryWrapper<WebsiteFile> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(websiteFile.getFileName())) {
            queryWrapper.eq(WebsiteFile::getFileName, websiteFileReqVo.getFileName());
        }
        queryWrapper.orderByDesc(WebsiteFile::getCreateTime);
        IPage<WebsiteFile> selectResult = websiteFileMapper.selectPage(new Page<>(current, size, true), queryWrapper);
        List<WebsiteFileRespVo> resultList = selectResult.getRecords().stream()
                .map(bean -> {
                    WebsiteFileRespVo websiteFileRespVo = Convert2Bean.convertBean(bean, WebsiteFileRespVo.class);
//                    websiteContentRespVo.setCreateUser(userService.getById(websiteContentRespVo.getCreateUser()).getName());
                    return websiteFileRespVo;
                })
                .collect(Collectors.toList());
        Page<WebsiteFileRespVo> page = new Page<>(current, size, true);
        page.setCurrent(selectResult.getCurrent());
        page.setTotal(selectResult.getTotal());
        page.setRecords(resultList);
        return page;
    }

    @Override
    public WebsiteFileRespVo queryById(String id) {
        WebsiteFile websiteFile = baseMapper.selectById(id);
        return Convert2Bean.convertBean(websiteFile, WebsiteFileRespVo.class);
    }

    @Override
    public Boolean addContent(WebsiteFileReqVo websiteFileReqVo) {
        websiteFileReqVo.setId("WB" + IdUtil.simpleUUID());
        WebsiteFile websiteFile = Convert2Bean.convertBean(websiteFileReqVo, WebsiteFile.class);
        return baseMapper.insert(websiteFile) > 0;
    }

    @Override
    public Boolean updateContent(WebsiteFileReqVo websiteFileReqVo) {
        WebsiteFile websiteFile = Convert2Bean.convertBean(websiteFileReqVo, WebsiteFile.class);
        LambdaUpdateChainWrapper<WebsiteFile> wrapper = new LambdaUpdateChainWrapper<>(websiteFileMapper);
        if (ObjectUtil.isNotEmpty(websiteFile.getFileUrl())) {
            wrapper.set(WebsiteFile::getFileUrl, websiteFile.getFileUrl());
        }
        if (ObjectUtil.isNotEmpty(websiteFile.getFileName())) {
            wrapper.set(WebsiteFile::getFileName, websiteFile.getFileName());
        }
        if (ObjectUtil.isNotEmpty(websiteFile.getSize())) {
            wrapper.set(WebsiteFile::getSize, websiteFile.getSize());
        }
        if (ObjectUtil.isNotEmpty(websiteFile.getOrganizedName())) {
            wrapper.set(WebsiteFile::getOrganizedName, websiteFile.getOrganizedName());
        }
        if (ObjectUtil.isNotEmpty(websiteFile.getIcon())) {
            wrapper.set(WebsiteFile::getIcon, websiteFile.getIcon());
        }
        if (ObjectUtil.isNotEmpty(websiteFile.getIconFlag())) {
            wrapper.set(WebsiteFile::getIconFlag, websiteFile.getIconFlag());
        }
        wrapper.eq(WebsiteFile::getId, websiteFile.getId());
        return wrapper.update();
    }

    @Override
    public Boolean deleteContent(String id) {
        return websiteFileMapper.deleteById(id) > 0;
    }


}
